package org.szgb.console.sys.custom.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.szgb.console.sys.custom.entity.SysCustomFormRecordItem;
import org.szgb.console.sys.custom.mapper.SysCustomFormRecordItemMapper;
import org.szgb.console.utils.Page;
import org.szgb.console.utils.UUIDUtil;

import java.util.List;

@Slf4j
@Service
public class SysCustomFormRecordItemService {
    private final SysCustomFormRecordItemMapper sysCustomFormRecordItemMapper;

    @Autowired
    public SysCustomFormRecordItemService(SysCustomFormRecordItemMapper sysCustomFormRecordItemMapper){
        this.sysCustomFormRecordItemMapper = sysCustomFormRecordItemMapper;
    }

    @Transactional(readOnly = true)
    public SysCustomFormRecordItem get(String id) {
        return sysCustomFormRecordItemMapper.get(id);
    }

    @Transactional(readOnly = true)
    public List<SysCustomFormRecordItem> findList(SysCustomFormRecordItem sysCustomFormRecordItem) {
        if (sysCustomFormRecordItem != null && sysCustomFormRecordItem.getPageDto() != null) {
            Page<SysCustomFormRecordItem> page = new Page<>(sysCustomFormRecordItem.getPageDto());
            page.setFirst((page.getPageNo() - 1) * page.getPageSize());
            sysCustomFormRecordItem.setPage(page);
        }
        return sysCustomFormRecordItemMapper.findList(sysCustomFormRecordItem);
    }

    @Transactional(readOnly = true)
    public Integer getCount(SysCustomFormRecordItem sysCustomFormRecordItem) {
        return sysCustomFormRecordItemMapper.getCount(sysCustomFormRecordItem);
    }

    @Transactional()
    public void save(SysCustomFormRecordItem sysCustomFormRecordItem) {
        // 检查实体对象的ID是否为null，判断是否为新实体
        if (sysCustomFormRecordItem.getId() == null || sysCustomFormRecordItem.getId().isEmpty()) {
            // 对于新实体，设置创建者信息
            sysCustomFormRecordItem.setCreatedBy(sysCustomFormRecordItem.getUpdatedBy());
            // 添加UUID
            sysCustomFormRecordItem.setId(UUIDUtil.getUUID());
            // 执行插入操作
            sysCustomFormRecordItemMapper.insert(sysCustomFormRecordItem);
            // 返回成功插入的信息
        } else {
            // 执行更新操作
            sysCustomFormRecordItemMapper.update(sysCustomFormRecordItem);
        }
    }

    @Transactional
    public void delete(String ids) {
        sysCustomFormRecordItemMapper.delete(ids.split(","));
    }
}
